//
//  TransitionLayer.m
//  CarDemo
//
//  Created by huang kun on 8/12/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "TransitionLayer.h"
#import "MenuLayer.h"

@implementation TransitionLayer

- (void) dealloc {
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (id) init {
	if( (self = [super init]) ){
		
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		CCSprite *fabia1 = [CCSprite spriteWithFile:@"fabia01.png"];
		[self addChild:fabia1 z:4];
		[fabia1 setPosition:ccp(winSize.width/2, winSize.height/2)];
		
		CCSprite *fabia2 = [CCSprite spriteWithFile:@"fabia02.png"];
		[self addChild:fabia2 z:3];
		[fabia2 setPosition:ccp(winSize.width/2, winSize.height/2)];
		[fabia2 setOpacity:0];
		
		CCSprite *fabia3 = [CCSprite spriteWithFile:@"fabia03.png"];
		[self addChild:fabia3 z:2];
		[fabia3 setPosition:ccp(winSize.width/2, winSize.height/2)];
		[fabia3 setOpacity:0];

		CCSprite *fabia4 = [CCSprite spriteWithFile:@"startCar.png"];
		[self addChild:fabia4 z:1];
		[fabia4 setPosition:ccp(winSize.width/2, winSize.height/2)];
		[fabia4 setOpacity:0];

		
#define fadeTime 0.8f
#define stayTime 1.8f
		
		id fadeOut = [CCFadeOut actionWithDuration:fadeTime];
		id fadeIn = [CCFadeIn actionWithDuration:fadeTime];
		
		id delay = [CCDelayTime actionWithDuration:stayTime];
		id delay1 = [CCDelayTime actionWithDuration:stayTime+fadeTime];
		id delay2 = [CCDelayTime actionWithDuration:stayTime*2 + fadeTime*3];
		id delay3 = [CCDelayTime actionWithDuration:stayTime*3 + fadeTime*5];
		
		id callFunc = [CCCallFunc actionWithTarget:self selector:@selector(next:)];
		
		[fabia1 runAction:[CCSequence actions:delay, fadeOut,nil]];
		[fabia2 runAction:[CCSequence actions:delay1, fadeIn, [delay copy], [fadeOut copy],nil]];
		[fabia3 runAction:[CCSequence actions:delay2, [fadeIn copy], [delay copy], [fadeOut copy],nil]];
		[fabia4 runAction:[CCSequence actions:delay3,[fadeIn copy],callFunc,nil]];
	}
	return self;
}

- (void) next:(id)sender {
	[self setIsTouchEnabled:YES];
	CGSize winSize = [[CCDirector sharedDirector] winSize];
	CCSprite *label = [CCSprite spriteWithFile:@"touchScreen.png"];
	[self addChild:label z:5];
	[label setPosition:ccp(winSize.width/2, winSize.height/2)];
	[label runAction:[CCBlink actionWithDuration:500 blinks:1000]];
}


- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[MenuLayer node]]];
}


@end
